﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using mesRUpload.GenerateSFCWebservice;

namespace mesRUpload
{
    public partial class GenerateVC : Form
    {
        //private string GVC_WSDL = string.Empty;
        //private string GVC_TimeOut = string.Empty;
        //private string GVC_UserName = string.Empty;
        //private string GVC_Password = string.Empty;

        private string iniPath = AppDomain.CurrentDomain.BaseDirectory + "MesRUpload_MiFind.ini";

        private findCustomAndSfcDataRequest GVC_FindDataRequest;
        private MiConnRequest GVC_MiConnRequest;

        bool m_b_isOpen = false;

        private int code = 0;
        private string message = string.Empty;
        private string sfc = string.Empty;
        private string inventory = string.Empty;


        public GenerateVC()
        {
            InitializeComponent();
        }

        public GenerateVC(string txt)
        {
            InitializeComponent();

            GVC_G2_CB_modeProcessSFC.SelectedIndex = 0;
            GVC_G3_TB_CC.Text = txt;
            GVC_G1_TB_password.PasswordChar = '●';
            GVC_G1_PB_closeOpen.SizeMode = PictureBoxSizeMode.Zoom;
            GVC_G1_PB_closeOpen.Image = mesRUpload.Properties.Resources.close;

            initWebParams();

            initIni();
        }

        private void initWebParams()
        {
            GVC_MiConnRequest = new MiConnRequest();
            GVC_FindDataRequest = new findCustomAndSfcDataRequest();

            //GVC_WSDL = "http://ndmesd093.catlbattery.com:50000/atlmeswebservice/MiFindCustomAndSfcDataServiceService?wsdl";
            //GVC_TimeOut = "10000";
            //GVC_UserName = "SUP_TEST06";
            //GVC_Password = "Aa123456";

            GVC_FindDataRequest.site = "2001";
            GVC_FindDataRequest.user = "SUP_TEST06";
            GVC_FindDataRequest.operation = "ASSEM5";
            GVC_FindDataRequest.operationRevision = "#";
            GVC_FindDataRequest.activity = "EAP_WS";
            GVC_FindDataRequest.resource = "SCWM1002";
            GVC_FindDataRequest.inventory = " ";
            GVC_FindDataRequest.findSfcByInventory = false;
            GVC_FindDataRequest.modeProcessSFC = modeProcessSFC.MODE_NONE;
            GVC_FindDataRequest.sfc = "Z10100025";

        }

        private void initIni()
        {
            if (!INIConfigAPI.InitPath(iniPath))
            {
                return;
            }

            initCtrlsText();
        }

        private void initCtrlsText()
        {
            GVC_G1_TB_wsdl.Text = INIConfigAPI.ReadWebservice(
                INIConfigAPI.webservice_id.MESWSDL);
            GVC_G1_TB_timeout.Text = INIConfigAPI.ReadWebservice(
                INIConfigAPI.webservice_id.TimeOut);
            GVC_G1_TB_user.Text = INIConfigAPI.ReadWebservice(
                INIConfigAPI.webservice_id.User);
            GVC_G1_TB_password.Text = EnDecryptAPI.DeCrypt(INIConfigAPI.ReadWebservice(
                INIConfigAPI.webservice_id.Password), "mesrupload");

            GVC_G2_TB_site.Text = INIConfigAPI.ReadRequest(
                INIConfigAPI.request_id.site);
            GVC_G2_TB_activityID.Text = INIConfigAPI.ReadRequest(
                INIConfigAPI.request_id.activityId);
            GVC_G2_TB_operation.Text = INIConfigAPI.ReadRequest(
                INIConfigAPI.request_id.operation);
            GVC_G2_TB_operationRevision.Text = INIConfigAPI.ReadRequest(
                INIConfigAPI.request_id.operationRevision);
            GVC_G2_TB_resource.Text = INIConfigAPI.ReadRequest(
                INIConfigAPI.request_id.resource);
            GVC_G2_TB_operationUser.Text = INIConfigAPI.ReadRequest(
                INIConfigAPI.request_id.user);
            switch (INIConfigAPI.ReadRequest(INIConfigAPI.request_id.modeProcessSfc))
            {
                case "COMPLETE_SFC":
                    GVC_G2_CB_modeProcessSFC.SelectedIndex = 2;
                    break;
                case "PASS_SFC":
                    GVC_G2_CB_modeProcessSFC.SelectedIndex = 3;
                    break;
                case "START_SFC":
                    GVC_G2_CB_modeProcessSFC.SelectedIndex = 1;
                    break;
                case "START_COMPLETE_SFC":
                    GVC_G2_CB_modeProcessSFC.SelectedIndex = 4;
                    break;
                case "NONE":
                default:
                    GVC_G2_CB_modeProcessSFC.SelectedIndex = 0;
                    break;
            }

        }

        private void GenerateVC_Load(object sender, EventArgs e)
        {
            //GVC_G3_TB_CC.Text = MESUpload.GVC_CCode;
        }

        //fixme: 生成待定
        private void GVC_G3_BTN_generate_Click(object sender, EventArgs e)
        {
            GVC_FindDataRequest.inventory = GVC_G3_TB_CC.Text;

            if (GVC_MiConnRequest.setupResponse(GVC_FindDataRequest, out code, out message, out inventory
                , out sfc) == CodeCollection.EXECUTE_SUCCESS)
            {
                GVC_G3_TB_sfc.Text = sfc;
                MESUpload.m_b_sfcIsNull = true;

                MessageBox.Show("虚拟码生成成功：\t" + sfc);
                MESUpload.m_VCode = sfc;

                this.Close();
            }
            else
            {
                GVC_G3_TB_sfc.Text = "sfc 虚拟码生成失败！";
                MESUpload.m_VCode = "";
            }
        }

        private void GVC_G1_BTN_setWebservice_Click(object sender, EventArgs e)
        {
            try
            {
                string GVC_WSDL = GVC_G1_TB_wsdl.Text;
                string GVC_UserName = GVC_G1_TB_user.Text;
                string GVC_Password = GVC_G1_TB_password.Text;
                string GVC_TimeOut = GVC_G1_TB_timeout.Text;

                long temp2 = GVC_MiConnRequest.SetConnectInfo(
                    GVC_WSDL, GVC_UserName, GVC_Password, GVC_TimeOut);

                if (CodeCollection.EXECUTE_SUCCESS
                    == temp2)
                {
                    INIConfigAPI.commPath = iniPath;
                    bool temp = INIConfigAPI.WriteWebservice(GVC_WSDL, GVC_TimeOut, GVC_UserName
                        , EnDecryptAPI.EnCrypt(GVC_Password, "mesrupload")); // 保存 ini
                    if (temp == true)
                    {
                        MessageBox.Show("webservice 服务设置成功！");
                    }
                    else
                    {
                        MessageBox.Show("webservice 服务设置成功，\r\n但保存 ini 失败，详情请看 MiFind 日志文档！");
                    }
                }
                else
                {
                    MessageBox.Show("设置失败，信息无效！");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("设置失败，信息无效！");
            }
        }

        private void GVC_G2_BTN_setRequest_Click(object sender, EventArgs e)
        {
            try
            {
                GVC_FindDataRequest.site = GVC_G2_TB_site.Text;
                GVC_FindDataRequest.user = GVC_G2_TB_operationUser.Text;
                GVC_FindDataRequest.operation = GVC_G2_TB_operation.Text;
                GVC_FindDataRequest.operationRevision = GVC_G2_TB_operationRevision.Text;
                GVC_FindDataRequest.activity = GVC_G2_TB_activityID.Text;
                GVC_FindDataRequest.resource = GVC_G2_TB_resource.Text;
                GVC_FindDataRequest.findSfcByInventory = true; // 默认查找 sfc
                GVC_FindDataRequest.inventory = GVC_G3_TB_CC.Text;

                switch (GVC_G2_CB_modeProcessSFC.SelectedItem.ToString()) // 默认过站模式为空
                {
                    case "START_SFC":
                        GVC_FindDataRequest.modeProcessSFC = modeProcessSFC.MODE_START_SFC;
                        break;
                    case "COMPLETE_SFC":
                        GVC_FindDataRequest.modeProcessSFC = modeProcessSFC.MODE_COMPLETE_SFC;
                        break;
                    case "PASS_SFC":
                        GVC_FindDataRequest.modeProcessSFC = modeProcessSFC.MODE_PASS_SFC;
                        break;
                    case "START_COMPLETE_SFC":
                        GVC_FindDataRequest.modeProcessSFC = modeProcessSFC.MODE_START_COMPLETE_SFC;
                        break;
                    case "NONE":
                    default:
                        GVC_FindDataRequest.modeProcessSFC = modeProcessSFC.MODE_NONE;
                        break;
                }

                INIConfigAPI.commPath = iniPath;
                bool temp = INIConfigAPI.WriteRequest(false, GVC_FindDataRequest.site
                    , GVC_FindDataRequest.user, GVC_FindDataRequest.operation
                    , GVC_FindDataRequest.operationRevision, GVC_FindDataRequest.activity
                    , GVC_FindDataRequest.resource
                    , GVC_G2_CB_modeProcessSFC.SelectedItem.ToString());  // 保存 ini

                if (temp == true)
                {
                    MessageBox.Show("request 请求设置成功！");
                }
                else
                {
                    MessageBox.Show("request 请求设置成功，\r\n但保存 ini 失败，详情请看 MiFind 日志文档！");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("设置失败，信息无效！");
            }

        }

        private void M_G1_PB_closeOpen_MouseClick(object sender, MouseEventArgs e)
        {
            GVC_G1_PB_closeOpen.SizeMode = PictureBoxSizeMode.Zoom;

            Image image1 = mesRUpload.Properties.Resources.close;

            if (m_b_isOpen)
            {
                image1 = mesRUpload.Properties.Resources.open;
                GVC_G1_TB_password.PasswordChar = new char();
                m_b_isOpen = false;
            }
            else
            {
                GVC_G1_TB_password.PasswordChar = '●';
                m_b_isOpen = true;
            }

            GVC_G1_PB_closeOpen.Image = image1;
        }

    }
}
